﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace yq.CacheHelp
{
    /// <summary>
    /// List 缓存
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class ListCaChe<T> where T : class ,new()
    {
        #region 用来确定缓存是否加载过

        private string tempkey = "_ListCaChe_" + typeof(T).FullName + "_";


        /// <summary>
        /// 获取当前缓存的配置
        /// </summary>
        /// <returns></returns>
        public ConfigHost GetConfig()
        {
            return yq.CaChe<T>.GetConfigHost();
        }

        #endregion

        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="item"></param>
        public void Add(T item)
        {
            yq.CaChe<T>.Client().LAdd(item);
        }

        /// <summary>
        /// 返回记录数量
        /// </summary>
        /// <returns></returns>
        public int Count()
        {
            return yq.CaChe<T>.Client().LCount();
        }

        /// <summary>
        /// 返回单条记录
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T GetModel(int index)
        {
           return yq.CaChe<T>.Client().LIndex(index);
        }

        /// <summary>
        /// 返回最后一行并且删除
        /// </summary>
        /// <returns></returns>
        public T GetPop()
        {
            return yq.CaChe<T>.Client().LPop();
        }

        /// <summary>
        /// 返回第一行并且删除
        /// </summary>
        /// <returns></returns>
        public T GetDequeue()
        {
            return yq.CaChe<T>.Client().LDequeue();
        }
     
        /// <summary>
        /// 对一个列表进行修剪(trim)，就是说，让列表只保留指定区间内的元素，不在指定区间之内的元素都将被删除。
        /// 举个例子，执行命令 LTRIM list 0 2 ，表示只保留列表 list 的前三个元素，其余元素全部删除。
        /// </summary>
        /// <param name="stop"></param>
        /// <param name="end"></param>
        public void Trim(int stop, int end)
        {
            yq.CaChe<T>.Client().LTrim(stop, end);
        }

        /// <summary>
        /// 返回所有
        /// </summary>
        /// <returns></returns>
        public IList<T> GetAll()
        {
           return  yq.CaChe<T>.Client().LALL();
        }

        /// <summary>
        /// 删除所有
        /// </summary>
        public void RemoveALL()
        {
           yq.CaChe<T>.Client().LRemoveALL();
        }

        /// <summary>
        /// 删除一条
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
           yq.CaChe<T>.Client().LRemoveAt(index);
        }
    }


}
